﻿// =================================================================== 
// Module说明
//====================================================================
// 创建时间：2018-06-08 16:43:21
// 负责人：Sushee
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq.Expressions;
using OF.Component.Base;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using OF.Component.Factory;
using OF.Component.DTO;

namespace OF.Component.BLL
{

    /// <summary>
    ///消息 扩展
    /// </summary>
    public partial class MessageOperate
    {
        /// <summary>
    	/// 根据断言条件获取单个消息
    	/// </summary>
        public static MessageInfo QuerySingle(Expression<Func<MessageInfo, bool>> predicate)
        {
            return dal.QuerySingle(predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取消息列表
    	/// </summary>
        public static IList<MessageInfo> QueryList(Expression<Func<MessageInfo, bool>> predicate)
        {
            return dal.QueryList(predicate);
        }

        /// <summary>
        /// 获取店铺消息
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public static List<OwnerMessageDto> SearchListPageByShopId(int shopId, int pageIndex, int pageSize, out int totalRecord)
        {
            var listReturn = new List<OwnerMessageDto>();
            /*return dal.SearchListPageByShopId(shopId, pageIndex, pageSize, out totalRecord)*/;
            var list = dal.SearchListFrontByShopId(shopId, pageIndex, pageSize, out totalRecord);
            list.ForEach(m =>
            {
                var dto=new OwnerMessageDto();
                var oTriggerShop = ShopOperate.GetByID(m.TriggerShopID);
                if (oTriggerShop != null)
                {
                    dto.ShopId = m.OwnerShopID;
                    dto.PostType = m.SubjectType;
                    dto.PostID = m.SubjectID;
                    dto.TriggerShopId = oTriggerShop.ShopID;
                    dto.TriggerShopMobile = oTriggerShop.Mobile;
                    dto.UserName = oTriggerShop.UserName;
                    dto.ShopName = oTriggerShop.ShopName;
                    if (oTriggerShop.HeadImgSrcType == 1)
                    {
                        dto.HeadImgSrc = oTriggerShop.HeadImgSrc;
                    }
                    else if (oTriggerShop.HeadImgSrcType == 2)
                    {
                        dto.HeadImgSrc = AppSettings.GetHttpFilePath(oTriggerShop.HeadImgSrc);
                    }
                    dto.CreatedDate = m.CreatedDate;
                    dto.MessageType = m.MessageType;
                    if (m.MessageType == 1)
                    {
                        var oComment = CommentOperate.GetByID(m.MessageContentID);
                        dto.CommentContent = oComment.Content;
                        dto.PostContent = GetPostContent(m);
                    }
                    else if (m.MessageType == 2)
                    {
                        dto.PostContent = GetPostContent(m);
                        dto.CommentContent = "赞了我";
                    }
                    else if (m.MessageType == 3)
                    {
                        dto.PostContent = GetPostContent(m);
                        dto.CommentContent = "打电话给我";
                    }
                    else if (m.MessageType == 4)
                    {
                        dto.PostContent = "查看我的店铺";
                        dto.CommentContent = "打电话给我";
                    }
                    listReturn.Add(dto);
                }
            });

            return listReturn;
        }
        private static string GetPostContent(MessageInfo m)
        {
            string szReturn = string.Empty;
            if (m.SubjectType == 1)
            {
                var oPost = PostSendOperate.GetByID(m.SubjectID);
                if (oPost != null)
                {
                    szReturn = oPost.Content;
                }
            }
            else if (m.SubjectType == 2)
            {
                var oPost = PostReceiveOperate.GetByID(m.SubjectID);
                if (oPost != null)
                {
                    szReturn = oPost.Content;
                }
            }
            return szReturn;
            ;
        }
        public static void SetMessageReaded(int shopId)
        {
            dal.SetMessageReaded(shopId);
        }
       
    }
}